using System;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml.Serialization;

namespace Nuwanda.TfsSpotlight.Data
{
    [Serializable]
    public class ChartOptions
    {
        [XmlElement]
        public List<string> RecentFieldsSelection { get; set; }

        private string _fieldName;
        [XmlElement]
        public string FieldName
        {
            get { return _fieldName; }
            set
            {
                _fieldName = value;
                SaveFieldSelection(value);
            }
        }

        [XmlElement]
        public SeriesChartType ChartType { get; set; }

        [XmlElement]
        public bool ShowIn3D { get; set; }

        [XmlElement]
        public bool SortByValueCount { get; set; }

        [XmlIgnore]
        public string ChartTypeView
        {
            get { return this.ChartType.ToString(); }
            set { this.ChartType = (SeriesChartType) Enum.Parse(typeof (SeriesChartType), value); }
        }

        [XmlIgnore]
        public bool AssignAlternatingColors
        {
            get { return this.ChartType != SeriesChartType.Pie && this.ChartType != SeriesChartType.Doughnut; }
        }

        public ChartOptions()
        {
            this.FieldName = null;
            this.ChartType = SeriesChartType.Pie;
            this.ShowIn3D = false;
            this.SortByValueCount = false;
            this.RecentFieldsSelection = new List<string>();
        }

        private void SaveFieldSelection(string fieldName)
        {
            if (String.IsNullOrEmpty(fieldName)) return;
            
            if (this.RecentFieldsSelection.Contains(fieldName) == false)
            {
                this.RecentFieldsSelection.Insert(0, fieldName);
                if(this.RecentFieldsSelection.Count > 3)
                {
                    this.RecentFieldsSelection.RemoveAt(this.RecentFieldsSelection.Count - 1);
                }
            }
            else
            {
                this.RecentFieldsSelection.Remove(fieldName);
                this.RecentFieldsSelection.Insert(0, fieldName);
            }
        }
    }
}
